package Controller;

import DAO.FoundRecordDAO;
import entity.FoundRecord;
import service.FoundRecordService;
import utils.Menu;
import java.sql.SQLException;
import java.util.Scanner;


public class FoundRecordController {
    private Scanner scanner = new Scanner(System.in);
    private FoundRecordService foundRecordService = new FoundRecordService("currentUser");
    public  void method() throws Exception {
        Menu.loginMenu();
        System.out.println("请输入操作: ");
        int choice = scanner.nextInt();
        scanner.nextLine();

        switch (choice) {
            case 1:
                addRecord();
                break;
            case 2:
                selectAllRecords();
                break;
            case 3:
                deleteRecord();
                break;
            case 4:
                updateRecord();
                break;
            case 5:
                checkRecordExists();
                break;
            case 0:
                System.out.println("感谢使用，再见！");
                System.exit(0);
            default:
                System.out.println("无效选择，请重新输入！");
                break;
        }

        if (choice >= 1 && choice <= 5) {
            System.out.print("\n按Enter键返回主菜单...");
            scanner.nextLine();
            method();
        }
    }
    private void addRecord() {
        try {
            System.out.println("\n--- 添加新记录 ---");
            FoundRecord record = new FoundRecord();

            System.out.print("输入用户ID: ");
            record.setUserId(scanner.nextInt());
            scanner.nextLine();

            System.out.print("输入物品ID: ");
            record.setItemId(scanner.nextInt());
            scanner.nextLine();

            System.out.print("输入拾获时间(YYYY-MM-DD HH:mm:ss): ");
            record.setFoundTime(scanner.nextLine());

            System.out.print("输入联系方式: ");
            record.setContactInfo(scanner.nextLine());

            System.out.print("输入状态(已归还或待认领): ");
            record.setStatus(scanner.nextLine());

            java.time.LocalDateTime now = java.time.LocalDateTime.now();
            String formattedDateTime = now.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            record.setCreatedAt(formattedDateTime);

            boolean success = foundRecordService.addFoundRecord(record);
            System.out.println(success ? "添加成功" : "添加失败");
        } catch (SQLException e) {
            System.out.println("添加记录失败: " + e.getMessage());
        }
    }

    private void selectAllRecords() {
        try {
            System.out.println("\n--- 所有记录 ---");
            foundRecordService.selectFoundRecord();
        } catch (Exception e) {
            System.out.println("查询记录失败: " + e.getMessage());
        }
    }

    private void deleteRecord() {
        try {
            System.out.println("\n--- 删除记录 ---");
            System.out.print("输入要删除的记录ID: ");
            int id = scanner.nextInt();
            scanner.nextLine();

            FoundRecord record = new FoundRecord();
            record.setId(id);

            if (!foundRecordService.existsRecord(record)) {
                System.out.println("记录不存在！");
                return;
            }

            boolean success = foundRecordService.deleteFoundRecord(record);
            System.out.println(success ? "删除成功" : "删除失败");
        } catch (SQLException e) {
            System.out.println("删除记录失败: " + e.getMessage());
        }
    }

    private void updateRecord() {
        try {
            System.out.println("\n--- 修改记录 ---");
            System.out.print("输入要修改的记录ID: ");
            int id = scanner.nextInt();
            scanner.nextLine();

            FoundRecord checkRecord = new FoundRecord();
            checkRecord.setId(id);
            if (!foundRecordService.existsRecord(checkRecord)) {
                System.out.println("记录不存在！");
                return;
            }

            FoundRecord record = new FoundRecord();
            record.setId(id);

            System.out.print("输入新的物品ID: ");
            String itemIdInput = scanner.nextLine();
            if (!itemIdInput.isEmpty()) {
                record.setItemId(Integer.parseInt(itemIdInput));
            }

            System.out.print("输入新的拾获时间(YYYY-MM-DD HH:mm:ss): ");
            String foundTime = scanner.nextLine();
            if (!foundTime.isEmpty()) {
                record.setFoundTime(foundTime);
            }

            System.out.print("输入新的联系方式: ");
            String contactInfo = scanner.nextLine();
            if (!contactInfo.isEmpty()) {
                record.setContactInfo(contactInfo);
            }

            System.out.print("输入新的状态: ");
            String status = scanner.nextLine();
            if (!status.isEmpty()) {
                record.setStatus(status);
            }

            boolean success = foundRecordService.updateFoundRecord(record);
            System.out.println(success ? "修改成功" : "修改失败");
        } catch (SQLException e) {
            System.out.println("修改记录失败: " + e.getMessage());
        } catch (NumberFormatException e) {
            System.out.println("输入格式错误: " + e.getMessage());
        }
    }

    private void checkRecordExists() {
        try {
            System.out.println("\n--- 检查记录是否存在 ---");
            FoundRecord record = new FoundRecord();

            System.out.print("输入记录ID: ");
            record.setId(scanner.nextInt());
            scanner.nextLine();

            boolean exists = foundRecordService.existsRecord(record);
            System.out.println("记录" + (exists ? "存在" : "不存在"));
        } catch (Exception e) {
            System.out.println("检查失败: " + e.getMessage());
        }
    }
    }




